'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'formdata', 'shiftformdata', 'validate', 'validate.en', 'datetimepicker'], function ($) {
    return {
        init: function (data) {
            this.data = data;
            this.confirmed = false;
            this.initPage();
        },

        initPage: function () {
            var that = this;
            $(document).on("change", 'input[name="existPassword"]', function(e){
              that.confirmed = false;
            })
            $(document).on("shown.bs.modal", "#edit-password", function (e) {

                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                
                $form.shiftform({
                    beforeSubmit: function (el) {
                        $form.trigger("validate.ajax.submit");
                        var result = $.bt_validate.result[$.bt_validate.form_id]
                        if(result) {
                          return that.confirmed ? true : that.validateOldPassword();
                        } else {
                         return false; 
                        }
                    }
                })
            });
            
            
            $(document).on("shown.bs.modal", "#upload-example", function (e) {
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftformdata()
              
            });
        },
        
        validateOldPassword: function() {
          var that = this;
          // 1. password and confirm password need same
          var password = $('input[name="password"]').val().trim();
          var confirmPassword = $('input[name="confirmPassword"]').val().trim();
          if (!(password === confirmPassword)) {
            var html = '<div class="tooltip fade bottom in" role="tooltip" id="confirm-password" style="display: block; top: 30px; left: 89.8281px;"><div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div><div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);"><i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;两次密码不一致！<div id="tlt_1500621070305" <="" div=""></div></div></div>';
            $(".role-confirm-password").append(html);
            setTimeout("$('#confirm-password').remove();", 1000);
            return false;
          }
        
          // 1. valid original password is right
          var existPassword = $('input[name="existPassword"]').val().trim();
          if (existPassword === "" || existPassword === undefined) {
            return false;
          }  
            
          $.ajax({
            url: window.__CTX__.root + "/profile/" + that.data.profileId + "/check-password?existPassword=" + existPassword,
            type: 'GET'
          }).done(function(res){
            
            res = typeof res === 'String' ? JSON.parse(res) : res
            
            if (res.data.validPassword == false) {
              var html = '<div class="tooltip fade bottom in" role="tooltip" id="tooltip-password" style="display: block; top: 30px; left: 89.8281px;"><div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div><div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);"><i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;原密码不正确！<div id="tlt_1500611928313" <="" div=""></div></div></div>';
              $(".role-exist-password").append(html);
              setTimeout("$('#tooltip-password').remove();", 1000);
              return false;
            }
            
            that.confirmed = true;
            $("#edit-password form button[type='submit']").click();
            
          }).fail(function(){
            return false;
          })
        }

    }
})


                      
                                                

